{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b019bbcf",
   "metadata": {},
   "source": [
    "### 课程内容\n",
    "* 常用模块\n",
    "* for 循环\n",
    "* for 循环 配合内置函数 range（）的使用\n",
    "* jupyter notebook 安装插件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64d6b5b9",
   "metadata": {},
   "source": [
    "#### 常用模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e517b0c6",
   "metadata": {},
   "source": [
    "* datetime 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28459e66",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d6c58981",
   "metadata": {},
   "outputs": [],
   "source": [
    "# datetime.(按TAp键查询)\n",
    "# help(datetime)\n",
    "\n",
    "# 我们可以使用moudle_name.Tap键来查看该模块的所有方法，但不能查看所有方法的细则及使用场景，help 可以帮助我们来做这件事"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d1434075",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2021, 9, 17)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 里面的 date 方法\n",
    "datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1615aa0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 里面的 date 方法 取出年月日\n",
    "datetime.date.today().day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31095e46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "401975ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "50ec3e15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-09-17'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 中 isoformat 格式化年月日\n",
    "datetime.date.isoformat(datetime.date.today())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b707261d",
   "metadata": {},
   "source": [
    "* time 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8463ab2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "32ee920e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=9, tm_mday=17, tm_hour=10, tm_min=52, tm_sec=4, tm_wday=4, tm_yday=260, tm_isdst=0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 国际标准时间\n",
    "time.gmtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d64d9246",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=9, tm_mday=17, tm_hour=18, tm_min=52, tm_sec=30, tm_wday=4, tm_yday=260, tm_isdst=0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本地时间\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "94bc7945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'18:54'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime(\"%H:%M\")\n",
    "## 中间符号可自定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b737ad1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-09-17'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用time 的strftime方法 找到 年月日 然后拼接在一起即可\n",
    "time.strftime(\"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0e643d8a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'PM'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime(\"%p\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73082983",
   "metadata": {},
   "source": [
    "* random 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9e207edc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c06a5d46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7553084721396065"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (0,1)的随机数\n",
    "random.random()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "492af543",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1—26的随机数\n",
    "random.randint(1,26)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b619d28f",
   "metadata": {},
   "source": [
    "#### for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9666d5a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3bbb0cc1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)\n",
    "# i 既item 为变量\n",
    "# range()运行次数，默认由0开始，步长（step）为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f537f7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list为方法  与odds = [1,3,5,7...]相似\n",
    "list(range(5,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bba8125d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 3, 6, 9]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 步长设为3\n",
    "list(range(0,10,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0147718a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-10, -40, -70]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(-10,-100,-30))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91ab949a",
   "metadata": {},
   "source": [
    "#### for循环 配合 内置函数range（）的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8ccecd61",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = ['xiaoming','xiaohong','lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ed5b0311",
   "metadata": {},
   "outputs": [],
   "source": [
    "# enumerate 枚举 产生steps的位置索引值（index）\n",
    "# for i in names : 取到了names中的值（values）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3ce951f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiaoming---20210001\n",
      "xiaohong---20210002\n",
      "lihua---20210003\n"
     ]
    }
   ],
   "source": [
    "# for 遍历数据的重要操作\n",
    "for k,i in enumerate(names):\n",
    "    print(i+\"---\"+str(20210000+k+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0614d01e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例(优化)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a8dcfa3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This second seems a little odd. ---当前时间节点的秒钟: 37\n",
      "等待的随机时间 14\n",
      "This second seems a little odd. ---当前时间节点的秒钟: 51\n",
      "等待的随机时间 16\n",
      "This second seems a little odd. ---当前时间节点的秒钟: 7\n",
      "等待的随机时间 12\n",
      "This second seems a little odd. ---当前时间节点的秒钟: 19\n",
      "等待的随机时间 43\n",
      "Not an odd second. ---当前时间节点的秒钟: 2\n",
      "等待的随机时间 47\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds= [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,57,59]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_second = datetime.today().second\n",
    "    if right_this_second in odds:\n",
    "        print(\"This second seems a little odd.\",\"---当前时间节点的秒钟:\",right_this_second)\n",
    "    else:\n",
    "        print(\"Not an odd second.\",\"---当前时间节点的秒钟:\",right_this_second)\n",
    "        # 增加等待时间\n",
    "    wait_time = random.randint(1,60)\n",
    "    print(\"等待的随机时间\",wait_time)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "695c631c",
   "metadata": {},
   "source": [
    "#### 安装插件\n",
    "* 可在[](pypi.org)输入插件名称进行下载\n",
    "* 下载方法： 打开rmd 输入pip install 黏贴网站所给的链接即可直接下载\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e79e697",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
